package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysMd;
import com.ruoyi.system.service.ISysMdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档信息
 *
 * @author renzhihao
 */
@Controller
@RequestMapping("/system/md")
public class SysMdController extends BaseController {
    private final String prefix = "system/md";

    @Autowired
    private ISysMdService mdService;


    @RequiresPermissions("system:md:view")
    @GetMapping()
    public String md() {
        return prefix + "/md";
    }

    @RequiresPermissions("system:md:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMd sysMd) {
        startPage();
        List<SysMd> list = mdService.selectMdList(sysMd);
        return getDataTable(list);
    }

    /**
     * 查询文档详细
     */
    @RequiresPermissions("system:md:list")
    @GetMapping("/view/{mdId}")
    public String view(@PathVariable("mdId") Long mdId, ModelMap mmap) {
        mmap.put("md", mdService.selectMdById(mdId));
        return prefix + "/view";
    }

    /**
     * 新增文档
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文档
     * @param sysMd 文档数据
     * @return 结果
     */
    @RequiresPermissions("system:md:add")
    @Log(title = "文档管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysMd sysMd) {
        if (!mdService.checkMdNameUnique(sysMd)) {
            return error("新增文档'" + sysMd.getMdName() + "'失败，文档名称已存在");
        }

        int rows = mdService.insertMd(sysMd);
        if (rows == -1) {
            return error("新增文档'" + sysMd.getMdName() + "'失败，文档名称已存在");
        }
        return toAjax(rows);
    }

    /**
     * 修改文档
     */
    @RequiresPermissions("system:md:edit")
    @GetMapping("/edit/{mdId}")
    public String edit(@PathVariable("mdId") Long mdId, ModelMap mmap) {
        mmap.put("md", mdService.selectMdById(mdId));

        return prefix + "/edit";
    }

    /**
     * 修改保存文档
     */
    @RequiresPermissions("system:md:edit")
    @Log(title = "文档管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysMd sysMd) {
        return toAjax(mdService.updateMd(sysMd));
    }

    /**
     * 删除文档
     */
    @RequiresPermissions("system:md:remove")
    @Log(title = "文档管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        Long[] mdIds = Convert.toLongArray(ids);
        if (mdIds.length < 1) {
            return AjaxResult.warn("请选择需要删除的数据");
        }

        return toAjax(mdService.deleteMdByIds(mdIds));
    }

    /**
     * 校验文档名称
     */
    @PostMapping("/checkMdNameUnique")
    @ResponseBody
    public boolean checkMdNameUnique(SysMd sysMd) {
        return mdService.checkMdNameUnique(sysMd);
    }

}